<%@ page session="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://myfaces.apache.org/trinidad" prefix="tr"%>
<%@ taglib uri="http://myfaces.apache.org/trinidad/html" prefix="trh"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>

<tr:panelGroupLayout>
<fieldset>
<legend>
	<tr:panelGroupLayout partialTriggers="type" layout="horizontal">
		<tr:outputText value="#{orderBean.offer ? 'oferta' : 'zamówienie' }" />
		<tr:outputFormatted value="&nbsp; #{orderBean.number} &nbsp;" rendered="#{orderBean.inserted}" inlineStyle="font-size:16px"/>
		
		<tr:commandLink shortDesc="Przekształć do zamówienia"
                				rendered="#{orderBean.inserted and orderBean.offer}" 
                				actionListener="#{orderController.makeOrderFromOffer}">
                <tr:image source="gfx/icon/offer-order.gif" />
        </tr:commandLink>
	</tr:panelGroupLayout>
</legend>

    <table border="0" style="width:100%;margin-bottom:10px;" cellspacing="0" cellpadding="2">    
        <tr>
        <td>
        </td>
        <td>
        <tr:panelGroupLayout layout="horizontal" rendered="#{!orderBean.inserted}">
                <tr:selectOneChoice id="type" label="Rodzaj"  immediate="true" inlineStyle="margin-right:10px"
                					value="#{orderBean.type}" autoSubmit="true" 
                					valueChangeListener="#{orderController.recordTypeChanged}">
                    <f:selectItems value="#{ConstantDictionaries.recordTypes}" />
                </tr:selectOneChoice>
                <tr:panelGroupLayout layout="horizontal" partialTriggers="type">
	            		<tr:selectOneChoice id="orderType" value="#{orderBean.number}" partialTriggers="type" inlineStyle="margin-right:10px"
	            							label="Rodzaj zamówienia" binding="#{orderController.orderNumberBinding}" 
	            							unselectedLabel="--- brak ---" required="true">
	                    	<f:selectItems value="#{ConstantDictionaries.orderTypes}" />
	                	</tr:selectOneChoice>	                	
	            </tr:panelGroupLayout>
	                        	
	    </tr:panelGroupLayout>
	    <tr:panelGroupLayout layout="horizontal" inlineStyle="width:100%;text-align:center">
	    
	    	    <tr:selectOneChoice label="Zamówienie NETTO/BRUTTO" value="#{orderBean.isNetAmount}"
	           								required="true" inlineStyle="margin-right:5px" unselectedLabel="--- brak ---"
            	 							rendered="#{empty orderBean.isNetAmount}">
            	 <f:selectItem itemValue="#{true}" itemLabel="NETTO" />
            	 <f:selectItem itemValue="#{false}" itemLabel="BRUTTO" />							
            	</tr:selectOneChoice>
	    
	                	
	             <tr:outputText value="Zamówienie w kwotach #{orderBean.isNetAmount ? 'NETTO': 'BRUTTO'}"
            					inlineStyle="font-size:16px" rendered="#{not empty orderBean.isNetAmount}"/>
            					
        </tr:panelGroupLayout>
	    

        </td>
        <td align="right">
            <tr:panelGroupLayout layout="horizontal">
        	<tr:commandLink shortDesc="Drukuj zamówienie do PDF" inlineStyle="margin-right:20px"
					action="#{orderController.printOrder}"
					rendered="#{orderBean.inserted}">
				<tr:image source="gfx/icon/print-icon.png" />
			</tr:commandLink>
            					
       	<tr:commandLink action="#{orderController.updateOrder}"
						 shortDesc="Zapisz zamówienie">
				<tr:image source="gfx/save_button.png" />
		</tr:commandLink>
		
		</tr:panelGroupLayout>
        
        	</td>
        </tr>
    	<tr class="table_row_header">
    		<td colspan="3">Dane osobowe</td>
    	</tr>
    	<tr>
            <td  align="right">
            
                            <tr:inputText id="clientName" required="true" readOnly="#{SessionDataTO.readOnly}" columns="40"
                              value="#{orderBean.client.name}" maximumLength="255" label="Nazwa klienta"
                             shortDesc="Imię i nazwisko osoby zamawiającej"/>
                <div style="width:120px">
                <tr:panelPopup shortDesc="Szukaj klienta" modal="true" title="Wyświetla okno wyszukiwania klienta" id="searchClientPopup"
    							triggerType="click" position="centered" text="Wyszukaj klienta"
    							inlineStyle="padding:2px 4px;border:1px solid gray;color:white;background-color:gray;display:block">
						<jsp:include page="/client_search_form.jsp" flush="true" />
				</tr:panelPopup>
				</div>
				
            </td>
            <td  align="right">
            
                <tr:inputText id="registrationdate" value="#{orderBean.registrationdate}" 
                				label="Data rejestracji" readOnly="true"
                				shortDesc="Registrationdate">
                    <f:convertDateTime pattern="dd-MM-yyyy HH:mm" locale="pl"/>
                </tr:inputText>

            </td>
            <td valign="top" align="right">
            
               <tr:selectOneChoice value="#{orderBean.status}" label="Status" 
                					readOnly="#{SessionDataTO.readOnly}" 
                					valueChangeListener="#{orderController.statusChanged}">
                    <f:selectItems value="#{ConstantDictionaries.orderStatus}" />
               </tr:selectOneChoice>
            </td>
        </tr>
        <tr>
            <td align="right" >
                  <tr:inputText id="contactPhone1" value="#{orderBean.client.phone}" label="Telefon kontaktowy"
                              required="true" readOnly="#{SessionDataTO.readOnly}" columns="40"
                              shortDesc="Telefon kontaktowy do osoby, odpowiedzialnej za zamówienie"/>                              
            </td>
            <td  align="right">
            
             <tr:inputText shortDesc="Osoba, ktora wprowadziła zamówienie" label="Wprowadził"
                				value="#{orderBean.creatorName}" readOnly="true"/>

            </td>
            <td align="right">
               <tr:panelGroupLayout layout="horizontal">
                    <tr:selectOneChoice value="#{orderBean.responsible}" label="Odpowiedzialna"
                                    readOnly="true"
                                    shortDesc="Wybierz osobę odpowiedzialną za to zamówienie">
                    <f:selectItems value="#{salesmanController.salesmanItems}"/>
                </tr:selectOneChoice>
    			<tr:panelPopup text="Przekaż" modal="true" title="Przekazanie zamówienia" id="passingOrderPopup"
    							triggerType="click" position="centered" 
    							inlineStyle="margin:2px;padding:2px 4px;border:1px solid gray;color:white;background-color:gray;display:block">
						<jsp:include page="/passing_form.jsp" flush="true" />
				</tr:panelPopup>
                </tr:panelGroupLayout>
            </td>
        </tr>
        <tr>
        <td align="right" >
        
                <tr:inputText id="email" value="#{orderBean.client.email}" label="email" columns="40" 
                				readOnly="#{SessionDataTO.readOnly}" maximumLength="150">
                    <f:validator validatorId="EmailValidator"/>
                </tr:inputText>

        </td>
        <td  align="right">
        <tr:panelGroupLayout layout="horizontal">
            <tr:selectBooleanCheckbox value="#{orderBean.approvedBoolean}" label="Zatwierdzone" 
                						shortDesc="Zatwierdzone"/>
                						
        	<tr:inputText id="approvedDate" value="#{orderBean.approved}" 
                				readOnly="true" rendered="#{not empty orderBean.approved}"
                				shortDesc="Data zatwierdzenia">
                    <f:convertDateTime pattern="dd-MM-yyyy HH:mm" locale="pl"/>
            </tr:inputText>
            </tr:panelGroupLayout>
        </td>
        <td align="right">
             
        		<tr:inputDate id="stuffOrdered" disabled="#{SessionDataTO.readOnly}"
                    		required="false" label="Towar zamówiono" 
                            value="#{orderBean.stuffOrdered}" />
        </td>
        </tr>

		<tr class="table_row_header">
			<td colspan="3">Odbiór</td>
		</tr>

        <tr>
            <td  align="right">
				<tr:selectOneRadio id="deliveryType" label="Odbiór" value="#{orderBean.deliveryType}" layout="horizontal" unselectedLabel="Osobisty">
					<f:selectItem itemLabel="Kurier" itemValue="kurier"/>
					<f:selectItem itemLabel="Poczta" itemValue="poczta"/>
				</tr:selectOneRadio>
            </td>
            <td  align="right">
         		<tr:inputDate id="shippingDispatchDate" label="Data nadania"
                            value="#{orderBean.shippingDispatchDate}"/>
            </td>
            <td rowspan="4" align="right">
                        
            <tr:inputText columns="40" rows="5" label="Adres wysyłki" id="shippingAddress"
                				value="#{orderBean.client.shippingAddress}" readOnly="#{SessionDataTO.readOnly}"
                				shortDesc="shippingAddress"/>
            </td>
        </tr>
        <tr>
            <td  align="right">
         		<tr:inputDate id="planDateOfReceiptInput" label="Planowana data odbioru"
                    		required="false"
                    		disabled="#{orderBean.statusEnum =='ODEBRANE' || orderBean.statusEnum =='ANULOWANE' || SessionDataTO.readOnly}"
                            value="#{orderBean.dateOfReceipt}"/>
            </td>
            <td  align="right">
                <tr:inputText id="shippingCost" value="#{orderBean.shippingCost}" readOnly="#{SessionDataTO.readOnly}"
                              shortDesc="Koszt ewentualnej przesyłki" label="Koszt przesyłki  #{orderBean.isNetAmount ? '(netto)': '(brutto)'}">
                    <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
                </tr:inputText>
            </td>
        </tr>
        <tr>
            <td  align="right">

            </td>
            <td  align="right">
            <tr:panelGroupLayout layout="horizontal">
                    <tr:inputText id="shippingNo" value="#{orderBean.shippingNo}" label="Numer przesyłki" 
                                  readOnly="#{SessionDataTO.readOnly}"
                                  shortDesc="Numer przesyłki" maximumLength="45"/>

                    <tr:commandButton shortDesc="Wyślij informację do klienta" icon="gfx/email-icon.png"
                                      actionListener="#{orderController.sendEmailToClient}"
                                      disabled="#{SessionDataTO.readOnly}" onclick="confirm('Zostanie wysłane potwierdzenie nadania przesyłki o numerze #{orderBean.shippingNo} na adres email klienta.')"
                                      inlineStyle="margin-left:5px;border:none;cursor:pointer"/>
            </tr:panelGroupLayout>
                
            </td>
        </tr>
        <tr>
            <td  align="right">

            </td>
            <td  align="right">
            </td>
        </tr>
        <tr class="table_row_header">
            <td colspan="3" align="left">
			Płatności
            </td>
        </tr>
          <tr>
            <td  align="right">
            
            <tr:panelGroupLayout layout="horizontal">
            
                <tr:inputText value="#{orderBean.netAmount}" readOnly="true" rendered="#{orderBean.isNetAmount}"
                				id="netAmount" label="Suma NETTO"  inlineStyle="margin-right:5px"
                				shortDesc="Suma całkowita NETTO">
                    <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
                </tr:inputText>
            
                <tr:inputText value="#{orderBean.totalamount}" readOnly="true"
                				id="totalAmount" label="Suma brutto"  
                				shortDesc="Suma całkowita BRUTTO">
                    <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
                </tr:inputText>
            </tr:panelGroupLayout>
            </td>
            <td  align="right">
                <tr:inputText id="receipt" rendered="#{not empty orderBean.number}" label="Paragon"
                              value="#{orderBean.receipt}" readOnly="#{SessionDataTO.readOnly}"
                              shortDesc="Paragon z kasy fiskalnej wpisany po zaksiegowaniu w systemie informatycznym"
                              maximumLength="45"/>
            </td>
            
            <td align="right">
                <tr:inputText maximumLength="50" id="vatInvoiceOu" value="#{orderBean.vatInvoiceProform}" 
                			  label="Faktura proforma" 
                              shortDesc="Numer faktury VAT proforma" readOnly="#{SessionDataTO.readOnly}"/>
            </td>

        </tr>
        <tr>
            <td  align="right">
		       <tr:inputText maximumLength="10" id="prepayment" value="#{orderBean.sumOfPayments}"
		                			  readOnly="true" label="Suma wpłat (brutto)"
		                              onkeypress="return checkField(window.self.event, NUMBER_FIELD)"
		                              shortDesc="" required="false">
		                    <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
		       </tr:inputText>
                
            </td>
            <td align="right"  >
                <tr:inputText id="preReceipt" readOnly="#{SessionDataTO.readOnly}"
                              value="#{orderBean.preReceipt}" label="Paragon zaliczkowy"
                               shortDesc="Paragon zaliczkowy musi być wpisany po wpłaceniu przez klienta zaliczki"
                               maximumLength="45" />
                			
            </td>
            <td align="right">
                <tr:inputText maximumLength="50" id="vatInvoiceOut" value="#{orderBean.vatInvoiceOut}" 
                			  label="Faktura dochodowa" 
                              shortDesc="Numer faktury VAT wychodzącej" readOnly="#{SessionDataTO.readOnly}"/>
            </td>
        </tr>
        <tr>
        <td align="right" >       
                   	<tr:inputText id="restForPay" value="#{orderBean.restOfPayment}"
                              label="Pozostało do zapłaty (brutto)" readOnly="true"
                               shortDesc="Reszta po odliczeniu zaliczki">
                    <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
            	</tr:inputText>
        </td>
        <td align="right" >
                        <tr:selectOneChoice value="#{orderBean.additionalStatus}"
        	    					readOnly="#{true}"
        	    					label="Status wpłat"
        	    					valueChangeListener="#{orderController.additionalStatusChanged}">
                    <f:selectItems value="#{ConstantDictionaries.additionalStatus}" />
                </tr:selectOneChoice>                			
       	</td>
        <td align="right">
                <tr:inputText maximumLength="45" id="vatInvoiceIn" value="#{orderBean.vatInvoiceIn}" 
                			  label="Faktura kosztowa" 
                              shortDesc="Numer faktury VAT zakupowej" readOnly="#{SessionDataTO.readOnly}"/>

        </td>
        </tr>
        <tr class="table_row_header">
            <td colspan="3" align="left">
			Inne
            </td>
        </tr>
        <tr>
        <td valign="top" width="40%">
        
            <tr:panelGroupLayout layout="horizontal" inlineStyle="width:100%">
    			<jsp:include page="/attachment_upload.jsp" flush="true"/>
    		</tr:panelGroupLayout>

        </td>
        <td colspan="2" align="right" valign="top">

			<tr:panelGroupLayout layout="horizontal" inlineStyle="width:100%">
		    	<jsp:include page="/payments.jsp" flush="true" />
		    </tr:panelGroupLayout>
		    
		    <tr:inputText columns="70" rows="7" label="Uwagi"
                			id="note" value="#{orderBean.note}" readOnly="#{SessionDataTO.readOnly}" 
                			shortDesc="Uwagi"/>                				
        </td>
        
        </tr>
    </table>
        
</fieldset>

<tr:panelGroupLayout>
<fieldset><legend>Artykuły</legend>
<tr:panelGroupLayout layout="horizontal">
	<tr:commandLink shortDesc="Dodaj artykuł" action="#{orderController.newProductItem}">
		<tr:image source="gfx/add_button.png" />
	</tr:commandLink>
</tr:panelGroupLayout>

    <t:dataTable id="productDataList" headerClass="table-header"
                 rowOnMouseOver="this.style.backgroundColor='#e0ffc2'" rowOnMouseOut="this.style.backgroundColor='#FFF'"
                 binding="#{orderController.productListDataTable}" width="100%" value="#{orderBean.productCollection}"
                var="product" rows="0" renderedIfEmpty="false">
    <t:column width="25%">
        <f:facet name="header" >
            <h:outputText value="Kod"/>
        </f:facet>
        <h:outputText value="#{product.code}"/>
    </t:column>
        <t:column width="70px">
            <f:facet name="header">
                <h:outputText value="Cena #{orderBean.isNetAmount ? '(netto)': '(brutto)'}" />
            </f:facet>
            <h:outputText value="#{product.unitPrice}" >
                <f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
            </h:outputText>
        </t:column>
        <t:column width="50px">
        <f:facet name="header">
            <h:outputText value="Ilość" />
        </f:facet>
        <h:outputText value="#{product.totalCount}"/>
    	</t:column>
        <t:column width="70px">
        <f:facet name="header">
            <h:outputText value="Razem #{orderBean.isNetAmount ? '(netto)': '(brutto)'}" />
        </f:facet>
        	<h:outputText value="#{product.unitPrice * product.totalCount}">
        		<f:convertNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" locale="pl"/>
        	</h:outputText>
    	</t:column>
        <t:column>
            <f:facet name="header">
                <h:outputText value="Uwagi" />
            </f:facet>
            <h:outputText value="#{product.note}"/>
        </t:column>
        <t:column style="width:25px" rendered="#{not SessionDataTO.readOnly}">
            <f:facet name="header">
                <h:outputText value="Edytuj"/>
            </f:facet>
            <tr:commandLink inlineStyle="margin-right:10px" 
            				shortDesc="Edytuj tę pozycję" 
            				action="#{orderController.editProductInOrder}">
                <tr:image source="gfx/edit.png" />
            </tr:commandLink>
        </t:column>
        <t:column style="width:30px" rendered="#{not SessionDataTO.readOnly}">
            <f:facet name="header">
                <h:outputText value="Usuń"/>
            </f:facet>
            <tr:commandLink shortDesc="Usuń tę pozycję z zamówienia"
            				onclick="return confirmMessageBox('Czy chcesz usunąć pozycję o kodzie: #{product.code}?');"
                             action="#{orderController.removeProductFromOrder}">
                <tr:image source="gfx/delete.png" />
            </tr:commandLink>
        </t:column>
    </t:dataTable>
</fieldset>
</tr:panelGroupLayout>

	<tr:panelGroupLayout layout="horizontal" inlineStyle="width:100%">
		<jsp:include page="/comment_list.jsp" flush="true" />
	</tr:panelGroupLayout>

</tr:panelGroupLayout>
